import ElementUI from 'element-ui'
import axios from 'axios';

var ajax = axios.create({
    baseURL: process.env.BASE_URL,
    headers: {
        APPID: 1,
    },
    withCredentials: true, //cookie
    crossDomain: true //跨域
});

//添加一个请求拦截器
ajax.interceptors.request.use(function(config) {
    //在请求发出之前进行一些操作
    if (sessionStorage.getItem('jwt')) {
        config.headers['TOKEN'] = sessionStorage.getItem('jwt');
    }
    return config;
}, function(err) {
    console.error(err);
    ElementUI.Message.error({
        message: '请求发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with request error
    return Promise.reject(err);
});
//添加一个响应拦截器
ajax.interceptors.response.use(function(res) {
    //在这里对返回的数据进行处理
    if (!res.data.status) {
        if (res.data.errorCode == 401) {
            location.href = '/login'
        }
        ElementUI.Message.error({
            message: res.data.message,
            type: 'error'
        });
        throw new Error(res.data.message);
    }

    return (res.data.data ? res.data.data : res.data.status);
}, function(err) {
    console.error(err);
    ElementUI.Message.error({
        message: '响应发生错误,请稍后再试!',
        type: 'error'
    });
    //Do something with response error
    return Promise.reject(err);
})


//管理员自身
export const adminLogin = params => {
    return ajax.post('adminControlPanel', params);
};
export const updatePwd = params => {
    return ajax.post('adminControlPanel/updatePassword', params);
};

//管理员操作
export const getAdminLists = params => {
    return ajax.get('admin', params);
}
export const addAdmin = params => {
    return ajax.post('admin', params);
}
export const deleteAdminById = adminId => {
    return ajax.delete('admin/' + adminId);
}
export const getAdminInfoById = adminId => {
    return ajax.get('admin/' + adminId);
}
export const updateAdminById = (adminId, params) => {
    return ajax.put('admin/' + adminId, params);
}

//团队管理
export const getTeamLists = params => {
    return ajax.get('team', params);
}
export const addTeamMenber = params => {
    return ajax.post('team', params);
}
export const updateTeamMenberById = params => {
    return ajax.put('team/' + params.teamId, params);
}
export const deleteTeamMenberById = teamId => {
    return ajax.delete('team/' + teamId);
}

//视频回顾
export const getVideoLists = params => {
    return ajax.get('periodVideo', params);
}
export const addVideo = params => {
    return ajax.post('periodVideo', params);
}
export const updateVideoById = params => {
    return ajax.put('periodVideo/' + params.periodVideoId, params);
}
export const deleteVideoById = periodVideoId => {
    return ajax.delete('periodVideo/' + periodVideoId);
}
//绩效
export const getPerformanceLists = params => {
    return ajax.get('performance', params);
}
export const addPerformance = params => {
    return ajax.post('performance', params);
}
export const updatePerformanceById = params => {
    return ajax.put('performance/' + params.performanceId, params);
}
export const deletePerformanceById = performanceId => {
    return ajax.delete('performance/' + performanceId);
}
//节目时间设置
export const getTimeLists = params => {
    return ajax.get('courseTime', params);
}
export const addTime = params => {
    return ajax.post('courseTime', params);
}
export const updateTimeById = params => {
    return ajax.put('courseTime/' + params.courseTimeId, params);
}
export const deleteTimeById = courseTimeId => {
    return ajax.delete('courseTime/' + courseTimeId);
}

//节目管理
export const getCourseLists = params => {
    return ajax.get('course', params);
}
export const addCourse = params => {
    return ajax.post('course', params);
}
export const updateCourseById = params => {
    return ajax.put('course/' + params.courseId, params);
}
export const deleteCourseById = courseId => {
    return ajax.delete('course/' + courseId);
}

//文件上传 前台文件需要设置一个path属性
export const uploadFiles = params => {
    return ajax.post('upload' + params.path, params);
};

//自定义数据
export const getConfig = keys => {
    return ajax.get('config?keys=' + keys);
}
export const setConfig = params => {
    return ajax.post('config', params);
}

//聊天消息
export const getMessagesList = params => {
    return ajax.get('message', params);
}
export const postMessage = params  => {
    return ajax.post('message', params);
}
export const messagePass = messageId  => {
    return ajax.put('message/' + messageId);
}
export const deleteMessageById = messageId  => {
    return ajax.delete('message/' + messageId);
}

//文章分类
export const getArticleCategoryList = params => {
    return ajax.get('articleCategory', params);
};
export const addArticleCategory = params => {
    return ajax.post('articleCategory', params);
};
export const deleteArticleCategoryById = id => {
    return ajax.delete('articleCategory/' + id);
};
export const updateArticleCategoryById = params => {
    return ajax.put('articleCategory/' + params.articleCategoryId, params);
};

//文章列表
export const getArticleList = parms => {
    return ajax.get('article', parms);
};
export const addArticle = params => {
    return ajax.post('article', params);
};
export const deleteArticleById = id => {
    return ajax.delete('article/' + id);
};
export const getArticleById = id => {
    return ajax.get('article/' + id);
};
export const updateArticleById = params => {
    return ajax.put('article/' + params.articleId, params);
};

//活动banner
export const getAdBannerList = parms => {
    return ajax.get('banner', parms);
};
export const addAdBanner = params => {
    return ajax.post('banner', params);
};
export const updateAdBanner = params => {
    return ajax.put('banner/' + params.bannerId, params);
};
export const deleteAdBanner = bannerId => {
    return ajax.delete('banner/' + bannerId);
};
